package com.good.child;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.core.app.ActivityCompat;

import com.good.child.config.Constants;
import com.good.child.ui.activity.LoginActivity;
import com.good.child.ui.activity.WelcomeActivity;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.BasePresenter;
import com.yimidida.library.base.ICallBackListener;
import com.yimidida.library.base.LibraryApplication;
import com.yimidida.library.tool.TActivityUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/2/24
 * by Mr.Zhu
 * Description: 启动页面
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.id_launch_iv)
    ImageView mLaunchImage;
    @BindView(R.id.btn_skip)
    Button skipBtn;
    private boolean isLogin = false;
    private int index = 0;


    @Override
    protected BasePresenter initPresenter() {
        return new BasePresenter();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        setStatusBar(true, false);
        setAndroidNativeLightStatusBar(this, true);
//        isLogin = UserLogical.getInstance().isLogin();
        requestPermission();
//        initCount();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

    private void requestPermission() {
//        RxPermissions rxPermissions = new RxPermissions(this);
//        rxPermissions.request(android.Manifest.permission.CAMERA,
//                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                        android.Manifest.permission.READ_EXTERNAL_STORAGE,
//                        android.Manifest.permission.READ_PHONE_STATE,
//                        android.Manifest.permission.ACCESS_FINE_LOCATION,
//                        android.Manifest.permission.ACCESS_COARSE_LOCATION,
//                        android.Manifest.permission.MANAGE_EXTERNAL_STORAGE
//                )//Manifest.permission.ACCESS_FINE_LOCATION
//                .subscribe(granted -> {
//                    if (granted) {
//                        initCount();
//                    } else {
//                        showConfirmCancelDialog(getString(R.string.tips), getString(R.string.permission_content), new ICallBackListener() {
//                            @Override
//                            public void onCallBack() {
//                                finish();
//                            }
//                        });
//                    }
//                });

        ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
//                .permission(Permission.ACCESS_BACKGROUND_LOCATION)
        XXPermissions.with(this)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .permission(Permission.CAMERA)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            initCount();
                        } else {
                            if (permissions.contains(Permission.ACCESS_FINE_LOCATION)
                                    || permissions.contains(Permission.ACCESS_COARSE_LOCATION)
                                    || permissions.contains(Permission.MANAGE_EXTERNAL_STORAGE)) {
                                initCount();;
                            } else {
                                showConfirmCancelDialog(getString(R.string.tips), getString(R.string.permission_content), new ICallBackListener() {
                                    @Override
                                    public void onCallBack() {
                                        finish();
                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            XXPermissions.startPermissionActivity(SplashActivity.this, permissions);
                        }
                    }
                });
    }


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                skipBtn.setText(getString(R.string.skip) + " (" + getCount() + ")");
                handler.sendEmptyMessageDelayed(0, 1000);
            }
            return true;
        }
    });

    public int getCount() {
        index--;
        if (index < 0) {
            index = 0;
        }
        if (index == 0) {
            jumpLogin();
        }
        return index;
    }

    private void jumpLogin() {
//        TActivityUtils.jumpToActivity(SplashActivity.this, CalculatorActivity.class);
        boolean first = (boolean) LibraryApplication.getSharedata().getData(Constants.FIRST_LOGIN, true);
        if (first) {
            TActivityUtils.jumpToActivity(SplashActivity.this, WelcomeActivity.class);
        } else {
            String telephone = (String) LibraryApplication.getSharedata().getData(Constants.LOGIN_TELEPHONE, "");
            String deviceCode = (String) LibraryApplication.getSharedata().getData(Constants.DEVICE_CODE, "");
            if (TextUtils.isEmpty(telephone) || TextUtils.isEmpty(deviceCode) ) {
                TActivityUtils.jumpToActivity(SplashActivity.this, LoginActivity.class);
            } else {
                TActivityUtils.jumpToActivity(SplashActivity.this, MainActivity.class);
            }
        }
        this.finish();
    }

    private void initCount() {
        skipBtn.setVisibility(View.VISIBLE);
        skipBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpLogin();
                finish();
            }
        });
        index = 3;
        skipBtn.setText(getString(R.string.skip) + " (" + index + ")");
        handler.sendEmptyMessageDelayed(0, 1000);
    }
}
